/*
 * Copyright (c) 2008, Yifu Huang
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of SleekIce nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package demo.examples;

import com.sleekice.core.GameSpecs;
import com.sleekice.core.SleekCore;
import com.sleekice.gui.Button;
import com.sleekice.gui.GuiHandler;
import com.sleekice.gui.MessageDialog;
import com.sleekice.gui.ScrollBar;
import com.sleekice.gui.TextArea;
import com.sleekice.gui.TextButton;
import com.sleekice.gui.TextField;
import com.sleekice.input.InputAction;
import com.sleekice.input.KeyboardInput;
import com.sleekice.input.MouseInput;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;

public class Gui extends SleekCore {
    
    private GameSpecs specs = new GameSpecs();
    
    private MouseInput mInput = new MouseInput();
    
    private Button button = new TextButton("JavaMan22",100,100);
    private Button button2 = new TextButton("r0bby",100,130);
    private Button button3 = new TextButton("mcsmurf",100,160);
    
    private Button button4 = new TextButton("cheeser",220,100);
    private Button button5 = new Button(220,130,50,50);
    private Button button6 = new Button(220,190,50,50);
    
    private TextArea textArea = new TextArea(340,50,200,150);
    
    private ScrollBar bar = new ScrollBar(100,300,20,200);
    
    private KeyboardInput kInput = new KeyboardInput();
    private InputAction quitGame = new InputAction();
    
    private TextField nameField = new TextField(300,300,150,20);
    private TextField passField = new TextField(300,330,150,20);
    
    private GuiHandler guiHandler = new GuiHandler(this, kInput, mInput);
    
    public Gui() {
        guiHandler.addWidget(button);
        guiHandler.addWidget(button2);
        guiHandler.addWidget(button3);
        guiHandler.addWidget(button4);
        guiHandler.addWidget(button5);
        guiHandler.addWidget(button6);
        guiHandler.addWidget(nameField);
        guiHandler.addWidget(passField);
        guiHandler.addWidget(textArea);
        guiHandler.addWidget(bar);
        
        mInput.assignTo(this);
        
        button.resize(100, 20);
        button2.resize(100, 20);
        button3.resize(100, 20);
        button4.resize(100, 20);
        passField.setPassworded(true);
        
        textArea.addText("I like apples because they are fruity.");
        textArea.addText("I like fruity things because they are sweet.");
        textArea.addText("I like sweet things because they are cool.");
        textArea.addText("I like cool things because I hate heat.");
        textArea.addText("I hate heat because I hate heat.");
        textArea.addText("I like apples because I hate heat.");
        textArea.addText("Caution: reading this will delay thought growth...");
        textArea.addText("Read at own risk, it may destroy your life, maybe even relationships...");
        
        bar.assignTo(textArea);
        
        kInput.addTrigger(quitGame , KeyEvent.VK_ESCAPE);
        kInput.assignTo(this);
        
        specs.setTitle("GUI Demo");
        gameStart(specs);
    }

    public void onExit() {
    }

    public void update(double elapsedTime) {
        if (quitGame.isKeyPressed()) {
            stop();
        }
        
        mInput.update(elapsedTime);
        
        if (button.isClicked()) {
            guiHandler.addWidget(MessageDialog.getCentered(specs,"Hello World",200,200,MessageDialog.CONTINUE));
        }
        
        guiHandler.update(elapsedTime);
    }
    
    public void render(Graphics2D g) {
        /* clear the drawing surface */
        g.setColor(Color.gray);
        g.fillRect(0, 0, specs.getScreenWidth(), specs.getScreenHeight());
        
        guiHandler.render(g);
    }
    
    public static void main(String[] args) {
        new Gui();
    }
}
